﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Manager.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using ZDSoft.UPMS.Manager.SystemPart;
using ZDSoft.UPMS.Model.SystemPart;

namespace ZDSoft.UPMS.Component.BusinessPart
{
    public class OrganizationComponent : ComponentBase<Organization, OrganizationManager>, IOrganizationService
    {
        OrganizationManager a = new OrganizationManager();
        public string AddOrgain(string OrganlizationId, int Id)
        {
                    Organization organlizations = new Organization();
                    organlizations.Name = OrganlizationId;
                    organlizations.SubOrganization = a.FindByID(Id);
                    organlizations.Organizations = null;
                    a.Create(organlizations);
                    return organlizations.ID.ToString(); ;
                    
        }

        public void createOrg(string name)
        {
            IList<Organization> organlizationList = new List<Organization>();
            Organization organame = new Organization();
            organame.Name = name;
            a.Create(organame);
        }
        public int Create(string id, string pid, string name)
        {
            Organization Organizations = new Organization();
            IList<Organization> org = base.FindAll();
            bool flag = true;
            for (int i = 0; i < org.Count; i++)
            {
                if (org[i].ID == int.Parse(id))
                {

                    flag = false;
                    break;
                }
            }
            if (flag == true)
            {
                Organizations.Name = name;
                if (pid != "")
                {
                    Organizations.SubOrganization = a.FindByID(int.Parse(pid));
                }
                base.Create(Organizations);

            }
            else
            {
                Organizations = a.FindByID(int.Parse(id));
                Organizations.Name = name;
                a.Update(Organizations);
            }
            return Organizations.ID;
        }

        public void Create(Organization org, int id, string selectCoursID)
        {
            CourseManager d = new CourseManager();
            Organization b = a.FindByID(id);
            string[] select = selectCoursID.Split(',');
            //org.Courses = new List<Course>();
            foreach (string ids in select)
            {
                if (ids == "")
                {
                    continue;
                }

                Course cour = d.FindByID(int.Parse(ids));
                org.Name = b.Name;
                org.IsActive = b.IsActive;
                org.SubOrganization = b.SubOrganization;
                org.Organizations = b.Organizations;
                //org.Courses.Add(cour);
            }
            a.Update(org);
        }
        public void CreatTeacher(string SelectedTeacherId, int id, string selectedCourseId)
        {
            UserManager Teacher = new UserManager();
            string[] selectTeachers = SelectedTeacherId.Split(',');
            //初始化CourseMan ager
            CourseManager b = new CourseManager();
            //初始化Organization
            Organization org = new Organization();
            //初始化一个TeacherTeachingCourse集合
            Term currentTerm = new TermManager().GetCurrentTerm();

            //获取当前的课程及机构
            Course currentcur = b.FindByID(int.Parse(selectedCourseId));
            Organization currentOrag = a.FindByID(id);
            TeacherTeachingCourseManager m = new TeacherTeachingCourseManager();
            foreach (TeacherTeachingCourse t in currentOrag.TeacherTeachingCourses)
            {
                foreach (TeacherTeachingCourse c in currentcur.TeacherTeachingCourses)
                {
                    if (t.ID == c.ID)
                    {

                        m.Delete(t.ID);
                    }
                }
            }
            for (int i = 0; i < selectTeachers.Count(); i++)
            {
                if (selectTeachers[i] == "")
                {
                    continue;
                }
                else
                {
                    TeacherTeachingCourse cours = new TeacherTeachingCourse();
                    cours.Term = currentTerm;
                    cours.Course = b.FindByID(int.Parse(selectedCourseId));
                    cours.Classes = a.FindByID(id);
                    cours.Teacher = Teacher.FindByID(int.Parse(selectTeachers[i]));
                    m.Create(cours);
                }

            }
        }

        public void Creates(string selectedCourseId, int id)
        {

            //初始化CourseMan ager
            CourseManager b = new CourseManager();
            //初始化Organization
            Organization org = new Organization();
            //初始化一个TeacherTeachingCourse集合
            string[] select = selectedCourseId.Split(',');
            Term currentTerm = new TermManager().GetCurrentTerm();
            TeacherTeachingCourseManager m = new TeacherTeachingCourseManager();
            IList<TeacherTeachingCourse> tea = new List<TeacherTeachingCourse>();
            //获取当前的课程及机构
            Organization currentOrg = base.FindByID(id);
            foreach (TeacherTeachingCourse t in currentOrg.TeacherTeachingCourses)
            {
                m.Delete(t.ID);
            }
            for (int i = 0; i < select.Count(); i++)
            {
                if (select[i] == "")
                {
                    continue;
                }
                else
                {
                    if (currentOrg.Organizations.Count == 0)//配班级的课程
                    {
                        TeacherTeachingCourse cours = new TeacherTeachingCourse();
                        cours.Course = b.FindByID(int.Parse(select[i]));
                        cours.Term = currentTerm;
                        cours.Classes = a.FindByID(id);
                        m.Create(cours);
                    }

                    else//配非班级的课程
                    {
                        for (int j = 0; j < currentOrg.Organizations.Count; j++)
                        {   //获得该机构的子机构
                            if (currentOrg.Organizations[j].TeacherTeachingCourses != null &&
                                currentOrg.Organizations[j].TeacherTeachingCourses.Count > 0)
                            {
                                continue;
                            }
                            int ID = currentOrg.Organizations[j].ID;
                            TeacherTeachingCourse cours = new TeacherTeachingCourse();
                            cours.Course = b.FindByID(int.Parse(select[i]));
                            cours.Classes = a.FindByID(ID);
                            cours.Term = currentTerm;
                            m.Create(cours);
                        }
                        TeacherTeachingCourse courss = new TeacherTeachingCourse();
                        courss.Course = b.FindByID(int.Parse(select[i]));
                        courss.Classes = a.FindByID(id);
                        courss.Term = currentTerm;
                        m.Create(courss);
                    }
                }

            }
        }

        public IList<Organization> FindOrganization()
        {
            IList<Organization> org = new List<Organization>();
            return org;
        }

    }
}
